<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Login extends Controller
{
    public function index()
    {
        return $this->fetch('login/login');
    }

    public function login(Request $request)
    {
        $p=$request->post();
        $username = $p['username'];
        $password = md5($p['password']);
        $result = Db::name('admin_users')->where('username',$username)->where('password',$password)->find();
        if($result){
        	Session::set('admin_user',$result);
        	$id = $result['id'];
        	// var_dump($id);
        	$list = Db::name('user_roles')
                ->join('roles','user_roles.role_id = roles.id')
                ->join('role_powers','roles.id = role_powers.role_id')
                ->join('powers','role_powers.power_id = powers.id')
                ->where('user_roles.user_id',$id)->select();
            $power['0'] = 'Indexindex';
            foreach($list as $val){
            	$controller = $val['controller'];
            	$action = $val['action'];
            	$array = [$controller,$action];
            	//var_dump($array);
            	$power[] = implode('',$array);           	
            }
            //var_dump($power);
            Session::set('user_power',$power);
            return $this->error('登录成功!',url('/admin'));
            //var_dump($pow);
        }else{
        	return $this->error('账号或密码不存在!!!!!');
        }
    }

    public function quit()
    {
        Session::delete('admin_user');
        Session::delete('user_power');
    	return $this->success('登出成功!!!',url('/adminLoginIndex'));
    }
}
